﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Shop.Data
{
    public class AutoCompleteData
    {
        public static AutoCompleteStringCollection GetAvailableProducts(string inputText)
        {
            ShopDataClassesDataContext database = new ShopDataClassesDataContext();
            AutoCompleteStringCollection result = new AutoCompleteStringCollection();

            SearchSpecifications(inputText, database, result);

            SearchBarcodes(inputText, database, result);

            return result;
        }
  
        private static void SearchBarcodes(string inputText, ShopDataClassesDataContext database, AutoCompleteStringCollection result)
        {
            var databaseMatchesBarcode = database.ShopItems.Where(currentShopItem =>
                                                                  currentShopItem.Barcode.IndexOf(inputText) != -1);
            foreach (var match in databaseMatchesBarcode)
            {
                string stringToAdd = match.Barcode + " " + match.Specification;
                result.Add(stringToAdd);
            }
        }
  
        private static void SearchSpecifications(string inputText, ShopDataClassesDataContext database, AutoCompleteStringCollection result)
        {
            var databaseMatches = database.ShopItems.Where(currentShopItem =>
                                                           currentShopItem.Specification.IndexOf(inputText) != -1);
            foreach (var match in databaseMatches)
            {
                string stringToAdd = match.Specification + "->  Баркод \"" + match.Barcode + "\"";
                result.Add(stringToAdd);
            }
        }
    }
}
